//
//  FanButtonView.m
//  SimpleTest
//
//  Created by 谌涛 on 16/3/26.
//  Copyright © 2016年 www.zwang.com. All rights reserved.
//

#import "FanButtonView.h"
#define KTColorRGB_10(R,G,B) [UIColor colorWithRed:R/255. green:G/255. blue:B/255. alpha:1]
// 16进制颜色
#define KTColorRGB_16(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define STSCREEN_BOUNDS [UIScreen mainScreen].bounds
#define STSCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define STSCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define STMain_Tint KTColorRGB_16(0x1999FF)
#define KTGET_Image(name) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:nil]]


#define PI 3.14159265358979323846
#define kDegreesToRadians(degrees)  ((PI * degrees)/ 180)
@interface FanButtonView ()
{
    BOOL _haveShow;
    
    NSInteger _selectIndext;
    
}

@property (nonatomic, strong) NSMutableArray * shapeLayers;


@end

@implementation FanButtonView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchUpInstView:)];
        [self addGestureRecognizer:tap];
        self.backgroundColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
        [self startView:frame.size];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    if (_haveShow == NO) [self startView:self.bounds.size];
    self.clipsToBounds = YES;
    self.layer.cornerRadius = CGRectGetWidth(self.frame) / 2;
}
- (void)startView:(CGSize)selfSize
{
    _haveShow = YES;
    self.shapeLayers = [NSMutableArray array];
    
    UIColor * layerBackColor = KTColorRGB_10(243, 246, 249);
    
    self.backgroundColor = layerBackColor;
    
    CGFloat w_2 = CGRectGetWidth(self.bounds) / 2;
    CGFloat h = CGRectGetHeight(self.bounds);
    CGPoint layerCenter = CGPointMake(w_2, w_2);
    CGFloat pieRadius = w_2;
    
    for (int i = 0 ;i < 6;i ++) {
        CGFloat satrtAngle = i * M_PI/3 + M_PI/6;
        CGFloat endAngle = (i + 1) * M_PI/3 + M_PI/6;
        CAShapeLayer * shapeLayer = [self shapeLayerWithStartAngle:satrtAngle endAngle:endAngle];
        [self.layer insertSublayer:shapeLayer atIndex:0];
        [self.shapeLayers addObject:shapeLayer];
        UIView * dataView = [self dateViewGetWithTag:i];
        
        dataView.center = CGPointMake(w_2+w_2*0.75*cos(i*M_PI/3),w_2+w_2*0.75*sin(i*M_PI/3));
        [self addSubview:dataView];
    }
    // 中间白色
    CAShapeLayer * centerLayer = [CAShapeLayer layer];
    centerLayer.frame = CGRectMake(0, 0, pieRadius, pieRadius);
    centerLayer.position = layerCenter;
    centerLayer.backgroundColor = [UIColor whiteColor].CGColor;
    centerLayer.cornerRadius = pieRadius / 2;
    [self.layer addSublayer:centerLayer];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, pieRadius * 0.5, pieRadius * 0.5)];
    imageView.image = KTGET_Image(@"main_main.png");
    imageView.center = CGPointMake(w_2, h / 2 - pieRadius * 0.1);
    [self addSubview:imageView];
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame) + 2, w_2 * 2, pieRadius * 0.2)];
    title.font = [UIFont systemFontOfSize:16];
    title.text = @"全真模拟";
    title.adjustsFontSizeToFitWidth = YES;
    title.textAlignment = NSTextAlignmentCenter;
    [self addSubview:title];
    [self.shapeLayers insertObject:centerLayer atIndex:0];
    UIButton * centBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    centBtn.frame = CGRectMake(0, 0, pieRadius, pieRadius);
    centBtn.layer.cornerRadius = pieRadius / 2;
    centBtn.center = layerCenter;
    centBtn.layer.borderWidth=6;
    centBtn.layer.borderColor=KTColorRGB_10(243, 246, 249).CGColor;
    centBtn.clipsToBounds = YES;
    [centBtn addTarget:self action:@selector(clickCenterBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:centBtn];
}
- (UIView *)dateViewGetWithTag:(int)tag
{
    NSArray * imageNames = @[@"main_1.png",@"main_1.png",@"main_1.png",@"main_1.png",@"main_1.png",@"main_1.png"];
    NSArray * titleArr = @[@"无极剑圣",@"盲僧",@"战争女神",@"卡特琳娜",@"皮城女警",@"努克萨斯"];
    CGFloat pieRadius = CGRectGetWidth(self.bounds) * 0.2;
    UIView * dataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, pieRadius, pieRadius)];
    dataView.backgroundColor=[UIColor whiteColor];
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((pieRadius-30)/2, 5, 30, 30)];
    imageView.image = KTGET_Image(imageNames[tag]);
    [dataView addSubview:imageView];
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+5, pieRadius, pieRadius * 0.3)];
    title.font = [UIFont systemFontOfSize:14];
    title.text = titleArr[tag];
    title.adjustsFontSizeToFitWidth = YES;
    title.textAlignment = NSTextAlignmentCenter;
    [dataView addSubview:title];
    return dataView;
}
- (CAShapeLayer *)shapeLayerWithStartAngle:(CGFloat)startAnle endAngle:(CGFloat)endAngle
{ 
    CGPoint layerCenter = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
    CGFloat pieRadius = CGRectGetWidth(self.bounds) / 2+4;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    shapeLayer.lineWidth = 6;
    shapeLayer.strokeColor =  KTColorRGB_10(243, 246, 249).CGColor;
    shapeLayer.fillColor = [UIColor whiteColor].CGColor;
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:layerCenter];
    [path addArcWithCenter:layerCenter
                    radius:pieRadius
                startAngle:startAnle
                  endAngle:endAngle
                 clockwise:YES];
    [path addLineToPoint:layerCenter];
    shapeLayer.path = path.CGPath;
    return shapeLayer;
}

- (void)touchUpInstView:(UIGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:self];
    CGAffineTransform transform = CGAffineTransformIdentity;
    NSInteger index = -1;
    for (int i = 0; i < self.shapeLayers.count; i ++) {
        CAShapeLayer *shapeLayer = self.shapeLayers[i];
        if (CGPathContainsPoint(shapeLayer.path, &transform, location, 0)) {
            index = i;
            break;
        }
    }
    _selectIndext = index;
    if (index >= 0) {
        //if ([_deleaget respondsToSelector:@selector(selectNumberPa:)]) {
            NSLog(@"点击了第%lu个",index);
            
            //[_deleaget selectNumberPa:index];
        //}
    }
}
- (void)clickCenterBtn  {
    if ([_deleaget respondsToSelector:@selector(selectNumberPa:)]) {
        [_deleaget selectNumberPa:5];
    }
}

- (double)randians:(double)degress
{
    return  degress * PI / 180.0;
}

@end
